var branchCompanyData = [];
var chargingStationData = [];
var teamData = [];
var lineData = [];
var batteryTypeData = [];

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	addTitle("vehicleInfoSave");
	searchVehicleInfo();
})

function getChargingStation(data) {
	var length = data.length;
	chargingStationData = data;
	for (var i = 0; i < length; i++) {
		$("#chargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
}
function getBranchCompany(data) {
	var length = data.length;
	branchCompanyData = data;
	for (var i = 0;i < length; i++){
        $("#branchCompanyID").append("<option value='" + data[i].branchCompanyID + "'>" + data[i].branchCompanyName + "</option>");
    }
}
function changeSubBranchCompany(){
	var branchCompanyID = $("#branchCompanyID").val();
	if (branchCompanyID) {
		$("#subBranchCompanyID").empty();
		$("#teamID").empty();
		$("#lineID").empty();
		$("#subBranchCompanyID").append("<option value=''>请选择</option>");
		$("#subBranchCompanyID").val("");
		$ajaxList('subBranchCompany', getSubBranchCompany, {"branchCompanyID": branchCompanyID});
	} else {
		$("#subBranchCompanyID").empty();
		$("#teamID").empty();
		$("#lineID").empty();
	}
}
function getSubBranchCompany(data) {
	$("#subBranchCompanyID").empty();
	$("#subBranchCompanyID").append("<option value=''>请选择</option>");
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#subBranchCompanyID").append("<option value='" + data[i].subBranchCompanyID + "'>" + data[i].subBranchCompanyName + "</option>");
	}
}
function changeCarTeam(){
	var subBranchCompanyID = $("#subBranchCompanyID").val();
	if (subBranchCompanyID) {
		$("#teamID").empty();
		$("#lineID").empty();
		$("#teamID").append("<option value=''>请选择</option>");
		$("#teamID").val("");
		$ajaxList('carTeam', getCarTeam, {"subBranchCompanyID": subBranchCompanyID});
	} else {
		$("#teamID").empty();
		$("#lineID").empty();
	}
}
function getCarTeam(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#teamID").append("<option value='" + data[i].teamID + "'>" + data[i].teamName + "</option>");
	}
}
function changeLine(){
	var teamID = $("#teamID").val();
	var subBranchCompanyID = $("#subBranchCompanyID").val();
	if (teamID) {
		$("#lineID").empty();
		$("#lineID").append("<option value=''>请选择</option>");
		$("#lineID").val("");
		$ajaxList('lineName', getLineName,{"teamID": teamID, "subBranchCompanyID": subBranchCompanyID});
	} else {
		$("#lineID").empty();
	}
}
function getLineName(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#lineID").append("<option value='" + data[i].lineID + "'>" + data[i].lineName + "</option>");
	}
}
function getBatteryType(data) {
	var length = data.length;
	batteryTypeData = data;
	for (var i = 0;i < batteryTypeData.length;i++){
        $("#batteryType").append("<option value='" + batteryTypeData[i].batteryType + "'>" + batteryTypeData[i].batteryName + "</option>");  
	}
}
function searchVehicleInfo(){
	createComBody("detailOneBody", vehicleInfoTable, undefined, 1);
    createComBody("detailTwoBody", vehicleInfoTable, undefined, 2);
    $ajaxList('chargingStation', getChargingStation, {BulidStatus: '3'});
	$ajaxList('branchCompany', getBranchCompany);
	$ajaxList('batteryType', getBatteryType);
	selectDate("useDate", 0);
	selectDate("deliveryDate", 0, "top-right");
	setTimeout(hideLoading, 500);
}
function saveVehicleInfo(data){
     var result = data && data.result;
     var chargingStationID = data && data.chargingStationID;
     if (result == "Success") {
    	 showMessge(messageContent["Insert.Success"], undefined, goback);
     } else if (result == "NotFound"){
    	 showMessge(messageContent["Insert.Failed"]);
     } else if (result == "DriverNameExist"){
         showMessge(messageContent["Car.DriverNameExist"]);
     } else if (result == "TelephoneExist"){
    	 showMessge(messageContent["Car.TelephoneExist"]);
     } else if (result == "EmailExist"){
    	 showMessge(messageContent["Car.EmailExist"]);
     } else if (result == "WeChatExist"){
    	 showMessge(messageContent["Car.WeChatExist"]);
     } else {
    	 showMessge(messageContent["Insert.Error"]);
     }
}
function confirm(){
	return checkForm("saveForm", vehicleInfoTable);
}
function goback() {
	window.parent.skipMenu('vehicle/vehicleInfo', 'drownDown3', '', 'carPage', 'vehicleInfo');
}
